package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public ResponseResult findListByQuery(ChannelDTO dto) {
        //1.校验业务参数
        dto.checkParam();
        //2.执行分页条件查询
        //2.1 分页查询对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        //  判断前端是否传name 如果有值 添加模糊查询条件
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //  判断前端是否传status 如果有值 添加eq条件
        if (dto.getStatus() != null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //  添加排序条件
        queryWrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //3.封装通用分页返回结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.参数校验
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        //查询满足条件的数量
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        //2.保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3.返回封装结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        if (adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id为空");
        }
        AdChannel oldChannel = this.getById(adChannel.getId());
        String newName = adChannel.getName();
        if (StringUtils.isNotBlank(newName) && !newName.equals(oldChannel.getName())){
            if (newName.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newName));
            if (count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        //2.根据id修改数据
        this.updateById(adChannel);
        //3.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验（id 检查对应的数据状态是否为true）
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //判断频道是否存在
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"对应频道信息不存在");
        }
        //判断频道是否有效
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效，不允许删除");
        }
        //2.删除频道
        this.removeById(id);
//        if (id < 100){//如果id<100,认为是业务的错误
////            throw  new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
////            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"id不能小于100");
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
//        }

//        System.out.println(1/0);
        //3.返回结果
        return ResponseResult.okResult();
    }
}
